package com.zez.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zez.common.util.PageResult;
import com.zez.entity.Author;
import com.zez.entity.Book;
import com.zez.mapper.BookMapper;
import com.zez.service.IAuthorService;
import com.zez.service.IBookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zez.vo.BookVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zez
 * @since 2025-01-14
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements IBookService {
    @Autowired
    private IAuthorService authorService;

    @Override
    public PageResult<Book> findbypage(Integer page, Integer pagesize){
        Page<Book> page1 = this.page(new Page<>(page, pagesize));// 分页查询
        return new PageResult<>(page1.getRecords(), page1.getTotal());
    }

    @Override
    public PageResult<Book> search(Integer page, Integer pageSize, BookVO vo) {
        //  封装查询条件
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(vo.getBookname()), "bookname", vo.getBookname());
        wrapper.like(StrUtil.isNotBlank(vo.getType()), "type", vo.getType());
        wrapper.eq(false, "authorid", vo.getAuthorid());

        Page<Book> page1 = this.page(new Page<>(page, pageSize), wrapper);
        page1.getRecords().forEach(item -> {
            Author author = authorService.getById(item.getAuthorid());
            System.out.println(author.getAuthorid());
            item.setAuthorname(author.getAuthorname());
        });
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }
    @Override
    public boolean add(Book book){return this.save(book);}
}
